package com.hs.hlw.credit.system.business.service.impl;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.DataSourceService;
import com.hs.hlw.credit.system.common.enumeration.CommonStatus;
import com.hs.hlw.credit.system.data.primary.mapper.DataSourceMapper;
import com.hs.hlw.credit.system.data.primary.model.DataSource;
import com.hs.hlw.credit.system.data.primary.model.DataSourceExample;
import com.hualongdata.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * DataSourceServiceImpl
 * @author zhb
 * @date 2018/10/25
 */
@Service
public class DataSourceServiceImpl implements DataSourceService {
    private @Autowired DataSourceMapper dataSourceMapper;

    /**
     * 列表查询
     * @return
     */
    @Override
    public ResponseDto list(Short status) {
        DataSourceExample example = new DataSourceExample();
        DataSourceExample.Criteria criteria = example.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        List<DataSource> list = dataSourceMapper.selectByExample(example);

        return new ResponseDto("查询成功", list);
    }

    /**
     * 新增
     * @param dataSource
     * @return
     */
    @Override
    public ResponseDto add(DataSource dataSource) {
        dataSource.setCreateTime(new Date());
        dataSource.setStatus(CommonStatus.STATUS_1.code);
        dataSourceMapper.insertSelective(dataSource);
        return new ResponseDto("新增成功");
    }

    /**
     * 更新
     * @param dataSource
     * @return
     */
    @Override
    public ResponseDto update(DataSource dataSource) {
        dataSource.setUpdateTime(new Date());
        dataSourceMapper.updateByPrimaryKeySelective(dataSource);
        return new ResponseDto("新增成功");
    }

    /**
     * 删除
     * @param idList
     * @return
     */
    public ResponseDto delete(List<DataSource> idList) {
        if (idList != null && !idList.isEmpty()) {
            for (DataSource dataSource : idList) {
                dataSourceMapper.deleteByPrimaryKey(dataSource.getId());
            }
        }
        return new ResponseDto("删除成功");
    }


    /**
     * 更新状态
     * @param list
     * @return
     */
    public ResponseDto updateStatus(List<DataSource> list) {
        if (list != null && !list.isEmpty()) {
            for (DataSource dataSource : list) {
                if (dataSource.getId() == null) {
                    throw new BusinessException("存在未指定配置参数");
                }
                if (!CommonStatus.contains(dataSource.getStatus())) {
                    throw new BusinessException("存在未知的参数状态");
                }
            }
            for (DataSource dataSource : list) {
                DataSource dto = new DataSource();
                dto.setId(dataSource.getId());
                dto.setStatus(dataSource.getStatus());
                dto.setUpdateTime(new Date());
                dataSourceMapper.updateByPrimaryKeySelective(dto);
            }
        }
        return new ResponseDto("操作成功");
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public ResponseDto detail(Integer id) {
        DataSource dataSource = dataSourceMapper.selectByPrimaryKey(id);
        if (dataSource == null) {
            throw new BusinessException("记录不存在");
        }
        return new ResponseDto("查询成功", dataSource);
    }
}